﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
namespace Chenxin.Lib.House.DAL
{
    /// <summary>
    /// 数据访问类House_KeyReceiptionInfo。
    /// </summary>
    public class House_KeyReceiptionInfo
    {
        public House_KeyReceiptionInfo()
        { }
        #region  成员方法

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid ReceiptionId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from House_KeyReceiptionInfo where ReceiptionId=@ReceiptionId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ReceiptionId", DbType.Guid, ReceiptionId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.House.Entity.House_KeyReceiptionInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into House_KeyReceiptionInfo(");
            strSql.Append("ReceiptionId,ReceiptionCode,HouseId,HouseCode,HouseAddress,KeyCount,Status,GetKeyDay,UpdateTime,AddTime,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,AddPerson,AddPersonId)");

            strSql.Append(" values (");
            strSql.Append("@ReceiptionId,@ReceiptionCode,@HouseId,@HouseCode,@HouseAddress,@KeyCount,@Status,@GetKeyDay,@UpdateTime,@AddTime,@CityName,@CityId,@CompanyId,@CompanyName,@DepartId,@Depart,@AddPerson,@AddPersonId)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ReceiptionId", DbType.Guid, model.ReceiptionId);
            db.AddInParameter(dbCommand, "ReceiptionCode", DbType.AnsiString, model.ReceiptionCode);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "KeyCount", DbType.Int32, model.KeyCount);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "GetKeyDay", DbType.DateTime, model.GetKeyDay);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.House.Entity.House_KeyReceiptionInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update House_KeyReceiptionInfo set ");
            strSql.Append("ReceiptionCode=@ReceiptionCode,");
            strSql.Append("HouseId=@HouseId,");
            strSql.Append("HouseCode=@HouseCode,");
            strSql.Append("HouseAddress=@HouseAddress,");
            strSql.Append("KeyCount=@KeyCount,");
            strSql.Append("Status=@Status,");
            strSql.Append("GetKeyDay=@GetKeyDay,");
            strSql.Append("UpdateTime=@UpdateTime,");
            strSql.Append("AddTime=@AddTime,");
            strSql.Append("CityName=@CityName,");
            strSql.Append("CityId=@CityId,");
            strSql.Append("CompanyId=@CompanyId,");
            strSql.Append("CompanyName=@CompanyName,");
            strSql.Append("DepartId=@DepartId,");
            strSql.Append("Depart=@Depart,");
            strSql.Append("AddPerson=@AddPerson,");
            strSql.Append("AddPersonId=@AddPersonId");
            strSql.Append(" where ReceiptionId=@ReceiptionId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ReceiptionId", DbType.Guid, model.ReceiptionId);
            db.AddInParameter(dbCommand, "ReceiptionCode", DbType.AnsiString, model.ReceiptionCode);
            db.AddInParameter(dbCommand, "HouseId", DbType.Guid, model.HouseId);
            db.AddInParameter(dbCommand, "HouseCode", DbType.AnsiString, model.HouseCode);
            db.AddInParameter(dbCommand, "HouseAddress", DbType.AnsiString, model.HouseAddress);
            db.AddInParameter(dbCommand, "KeyCount", DbType.Int32, model.KeyCount);
            db.AddInParameter(dbCommand, "Status", DbType.Int32, model.Status);
            db.AddInParameter(dbCommand, "GetKeyDay", DbType.DateTime, model.GetKeyDay);
            db.AddInParameter(dbCommand, "UpdateTime", DbType.DateTime, model.UpdateTime);
            db.AddInParameter(dbCommand, "AddTime", DbType.DateTime, model.AddTime);
            db.AddInParameter(dbCommand, "CityName", DbType.AnsiString, model.CityName);
            db.AddInParameter(dbCommand, "CityId", DbType.Guid, model.CityId);
            db.AddInParameter(dbCommand, "CompanyId", DbType.Guid, model.CompanyId);
            db.AddInParameter(dbCommand, "CompanyName", DbType.AnsiString, model.CompanyName);
            db.AddInParameter(dbCommand, "DepartId", DbType.Guid, model.DepartId);
            db.AddInParameter(dbCommand, "Depart", DbType.AnsiString, model.Depart);
            db.AddInParameter(dbCommand, "AddPerson", DbType.AnsiString, model.AddPerson);
            db.AddInParameter(dbCommand, "AddPersonId", DbType.Guid, model.AddPersonId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid ReceiptionId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from House_KeyReceiptionInfo ");
            strSql.Append(" where ReceiptionId=@ReceiptionId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ReceiptionId", DbType.Guid, ReceiptionId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_KeyReceiptionInfo GetModel(Guid ReceiptionId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ReceiptionId,ReceiptionCode,HouseId,HouseCode,HouseAddress,KeyCount,Status,GetKeyDay,UpdateTime,AddTime,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,AddPerson,AddPersonId from House_KeyReceiptionInfo ");
            strSql.Append(" where ReceiptionId=@ReceiptionId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "ReceiptionId", DbType.Guid, ReceiptionId);
            Chenxin.Lib.House.Entity.House_KeyReceiptionInfo model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ReceiptionId,ReceiptionCode,HouseId,HouseCode,HouseAddress,KeyCount,Status,GetKeyDay,UpdateTime,AddTime,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,AddPerson,AddPersonId ");
            strSql.Append(" FROM House_KeyReceiptionInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        
        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageSize,int PageIndex,string strWhere)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("UP_GetRecordByPage");
            db.AddInParameter(dbCommand, "tblName", DbType.AnsiString, "House_KeyReceiptionInfo");
            db.AddInParameter(dbCommand, "fldName", DbType.AnsiString, "ID");
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "IsReCount", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "OrderType", DbType.Boolean, 0);
            db.AddInParameter(dbCommand, "strWhere", DbType.AnsiString, strWhere);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.House.Entity.House_KeyReceiptionInfo> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ReceiptionId,ReceiptionCode,HouseId,HouseCode,HouseAddress,KeyCount,Status,GetKeyDay,UpdateTime,AddTime,CityName,CityId,CompanyId,CompanyName,DepartId,Depart,AddPerson,AddPersonId ");
            strSql.Append(" FROM House_KeyReceiptionInfo ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.House.Entity.House_KeyReceiptionInfo> list = new List<Chenxin.Lib.House.Entity.House_KeyReceiptionInfo>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_House_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.House.Entity.House_KeyReceiptionInfo ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.House.Entity.House_KeyReceiptionInfo model = new Chenxin.Lib.House.Entity.House_KeyReceiptionInfo();
            object ojb;
            ojb = dataReader["ReceiptionId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.ReceiptionId = new Guid(ojb.ToString());
            }
            model.ReceiptionCode = dataReader["ReceiptionCode"].ToString();
            ojb = dataReader["HouseId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.HouseId = new Guid(ojb.ToString());
            }
            model.HouseCode = dataReader["HouseCode"].ToString();
            model.HouseAddress = dataReader["HouseAddress"].ToString();
            ojb = dataReader["KeyCount"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.KeyCount = (int)ojb;
            }
            ojb = dataReader["Status"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.Status = (int)ojb;
            }
            ojb = dataReader["GetKeyDay"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.GetKeyDay = (DateTime)ojb;
            }
            ojb = dataReader["UpdateTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.UpdateTime = (DateTime)ojb;
            }
            ojb = dataReader["AddTime"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddTime = (DateTime)ojb;
            }
            model.CityName = dataReader["CityName"].ToString();
            ojb = dataReader["CityId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CityId = new Guid(ojb.ToString());
            }
            ojb = dataReader["CompanyId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.CompanyId = new Guid(ojb.ToString());
            }
            model.CompanyName = dataReader["CompanyName"].ToString();
            ojb = dataReader["DepartId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.DepartId = new Guid(ojb.ToString());
            }
            model.Depart = dataReader["Depart"].ToString();
            model.AddPerson = dataReader["AddPerson"].ToString();
            ojb = dataReader["AddPersonId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.AddPersonId = new Guid(ojb.ToString());
            }
            return model;
        }

        #endregion  成员方法
    }
}

